/**
 * @作者 钟诚
 * @日期 2011-8-4
 * @时间 下午12:43:43
 * @描述
 * @版本
 */
package my.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

/**
 * @作者 钟诚
 * @日期 2011-8-4
 * @时间 下午12:43:43
 * @描述
 * @版本 v1.0
 */
public class FileUtil implements Constant {
	private static Log log = LogFactory.getLog(FileUtil.class);
	/**
	 * 
	 * @描述 复制文件
	 * @作者 钟诚
	 * @日期 2011-8-4
	 * @时间 下午12:42:16
	 * @param src
	 * @param dest
	 * @throws IOException
	 */
	public static void Copy(File src, File dest) throws IOException {
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			fis = new FileInputStream(src);
			bis = new BufferedInputStream(fis);

			fos = new FileOutputStream(dest);
			bos = new BufferedOutputStream(fos);

			byte[] b = new byte[1024];
			int len;
			while ((len = fis.read(b)) != -1) {
				bos.write(b, 0, len);
			}
		} finally {
			bos.close();
			fos.close();
			bis.close();
			fis.close();
		}
	}

	/**
	 * 
	 * @描述 文本文件读取
	 * @作者 钟诚
	 * @日期 2011-1-28
	 * @时间 下午03:01:36
	 * @param path
	 *            文件路径,例:"file/299_BKN00000000920110118.dat"
	 */
	public static String readTxtFile(String path) throws FileNotFoundException, IOException, Exception {
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			File file = new File(path);
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			StringBuffer text = new StringBuffer();
			String line = "";
			int lineNumber = 1;
			while ((line = br.readLine()) != null) {
				text.append(line);
				lineNumber++;
			}
			// if you want to get linenumber , then return linenumber
			return text.toString();
		} catch (FileNotFoundException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} finally {
			try {
				br.close();
				isr.close();
				fis.close();
			} catch (IOException e) {
				throw e;
			}
		}
	}
	/**
	 * 
	 * @param filePath
	 *            文件路径
	 * @return
	 */
	public static String getFileContent(File afile) {
		File file = afile;
		FileReader fr = null;
		BufferedReader reader = null;
		StringBuffer sb = new StringBuffer();
		String tmpStr = "";
		if (file.exists()) {
			try {
				fr = new FileReader(file);
				reader = new BufferedReader(fr);
				while ((tmpStr = reader.readLine()) != null) {
					sb.append(tmpStr);
					sb.append(System.getProperty("line.separator"));
				}
				return new String(sb.toString().getBytes("GBK"), "UTF-8");
			} catch (FileNotFoundException e) {
				log.error("---文件未找到", e);
			} catch (IOException e) {
				log.error("---读取异常", e);
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e) {
						log.error("---reader关闭异常", e);
					}
				}
				if (fr != null) {
					try {
						fr.close();
					} catch (IOException e) {
						log.error("---filereader关闭异常", e);
					}
				}
			}
		}
		return null;
	}

	public static String getXMLFileContent(File file) {
		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(file);
			return document.asXML();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @描述 删除文件(文件夹)
	 * @作者 钟诚
	 * @日期 2012-7-30
	 * @时间 下午02:01:52
	 * @param path
	 */
	public void deleteFile(String path){
		File file = new File(path);
		if(file.isFile()){
			file.delete();
		}else{
			for(File f:file.listFiles()){
				this.deleteFile(f.getAbsolutePath());
			}
			file.delete();
		}
	}
	public static void main(String[] args) throws FileNotFoundException, IOException, Exception {
		String a = readTxtFile("c:\\newtest.xml");
		System.out.println(a);
	}
}
